/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld.colorthreads;

import com.sun.spot.core.resources.transducers.ITriColorLED;
import com.sun.spot.core.resources.transducers.LEDColor;
import com.sun.spot.core.util.Utils;
import com.sun.spot.edemo.EDemoBoard;

/**
 *
 * @author me
 */
public class ColorBlinker {
    private ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
    private boolean[] flashLED = new boolean[8];
    private int[] currentColor = new int[3];
    private int[] blinkColor   = new int[3];

    private long timeOn;
    private long timeOff;

    public ColorBlinker(LEDColor col, boolean[] isOn, long on, long off) {
        init(col, isOn, on, off);
    }

    public ColorBlinker(LEDColor col, long on, long off) {
        boolean[] tmp = {true,true,true,true,true,true,true,true};
        init(col, tmp, on, off);
    }

    private void init(LEDColor col, boolean[] isOn, long delay, long off) {
        blinkColor[0] = col.red();
        blinkColor[1] = col.green();
        blinkColor[2] = col.blue();
        flashLED = isOn;
        timeOn = delay;
        timeOff= off;
    }

    private void turnOnDim() {
        for (int i=0; i<8; i++) {
            if(flashLED[i]) {
                readCurrentColor(i);
                currentColor[0]+=blinkColor[0]/2;
                currentColor[1]+=blinkColor[1]/2;
                currentColor[2]+=blinkColor[2]/2;
                writeCurrentColor(i);
            }
        }
    }

    private void turnOnBright() {
        for (int i=0; i<8; i++) {
            if(flashLED[i]) {
                readCurrentColor(i);
                currentColor[0]*=2;
                currentColor[1]*=2;
                currentColor[2]*=2;
                writeCurrentColor(i);
            }
        }
    }

    private void turnOff() {
        for (int i=0; i<8; i++) {
            if(flashLED[i]) {
                readCurrentColor(i);
                currentColor[0]-=blinkColor[0];
                currentColor[1]-=blinkColor[1];
                currentColor[2]-=blinkColor[2];
                writeCurrentColor(i);
            }
        }
    }

    public void pauseOff() {
        Utils.sleep(timeOff);
    }

    private void readCurrentColor(int index) {
        currentColor[0] = leds[index].getColor().red();
        currentColor[1] = leds[index].getColor().green();
        currentColor[2] = leds[index].getColor().blue();
    }

    private void writeCurrentColor(int index) {
        boundColors();
        leds[index].setColor(new LEDColor(currentColor[0], currentColor[1], currentColor[2]));
        leds[index].setOn();
    }

    private void boundColors() {
        for (int i=0; i<currentColor.length; i++) {
            currentColor[i] = Math.min(currentColor[i], 255);
            currentColor[i] = Math.max(currentColor[i], 0);
        }
    }

    public void flash() {
        turnOnDim();
        Utils.sleep(timeOn/2);
        turnOnBright();
        Utils.sleep(timeOn/2);
        turnOff();
    }
}
